package com.eedi.framework.user.controller.sys;

import com.eedi.framework.user.controller.sys.vo.*;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.user.controller.sys.vo.*;
import com.eedi.framework.user.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.eedi.framework.common.pojo.CommonResult.success;

/**
 * @author zybourn
 */
@Tag(name = "系统界面 - 用户&账号")
@RestController
@RequestMapping("/user/user-account")
@Validated
public class SysUserController {
    @Resource
    SysUserService sysUserService;

    @PostMapping("/create")
    @Operation(summary = "创建账号")
    @PreAuthorize("@sys.hasPermission('sys:user:create')")
    public CommonResult<SysUserLoginInfoResp> getAddressList(@RequestBody SysUserAccountCreateReq req) {
        return success(
                sysUserService.create(req)
        );
    }

    @PutMapping("/disable")
    @Operation(summary = "停用账号")
    @Parameter(name = "sysUserId", description = "账号编号", required = true)
    @PreAuthorize("@sys.hasPermission('sys:user:disable')")
    public CommonResult<Void> disable(@RequestParam("sysUserId") String sysUserId) {
        sysUserService.disableBySysUserAccountId(sysUserId);
        return success(null);
    }

    @PutMapping("/enable")
    @Operation(summary = "启用账号")
    @Parameter(name = "sysUserId", description = "账号编号", required = true)
    @PreAuthorize("@sys.hasPermission('sys:user:enable')")
    public CommonResult<Void> enable(@RequestParam("sysUserId") String sysUserId) {
        sysUserService.enableBySysUserAccountId(sysUserId);
        return success(null);
    }

    @GetMapping("/page")
    @Operation(summary = "账号列表")
    @PreAuthorize("@sys.hasPermission('sys:user:query')")
    public CommonResult<PageResult<SysUserLoginInfoResp>> query( SysUserAccountPageReq req) {
        return success(sysUserService.page(req));
    }

    @PutMapping("/update-password")
    @Operation(summary = "重置密码")
    @PreAuthorize("@sys.hasPermission('sys:user:update-password')")
    public CommonResult<String> updatePassword( @RequestParam("sysUserId") String sysUserId) {
        String defaultPasswd = sysUserService.resetPassword(sysUserId);
        return success(defaultPasswd);
    }

    @PutMapping("/reset-mobile")
    @Operation(summary = "修改手机号")
    @PreAuthorize("@sys.hasPermission('sys:user:reset-mobile')")
    public CommonResult<Void> resetMobile( @RequestBody SysUserAccountResetMobileReq req) {
        sysUserService.resetMobile(req);
        return success(null);
    }


    @PutMapping("/update")
    @Operation(summary = "修改用户信息")
    @PreAuthorize("@sys.hasPermission('sys:user:update')")
    public CommonResult<Void> update( @RequestBody SysUserUpReq req) {
        sysUserService.updateSysUserInfo(req);
        return success(null);
    }


}
